﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using VO;
using System.IO;
namespace ManagementBooks
{
    public partial class NhanVien : Form
    {
        public NhanVien()
        {
            InitializeComponent();
        }
        NhanVien_BUS _nahnvienBUS = new NhanVien_BUS();

        private void NhanVien_Load(object sender, EventArgs e)
        {
            Load_NV();
            SetAdd(false);
        }
        private void Load_NV()
        {
            DataTable dtTB = new DataTable();
            dtTB = _nahnvienBUS.allNhanVien();
            GrvDanhSachNV.Rows.Clear();
            if (dtTB.Rows.Count > 0)
            {
                foreach (DataRow row in dtTB.Rows)
                {
                    DataGridViewRow rows = new DataGridViewRow();
                    rows.CreateCells(GrvDanhSachNV);
                    rows.Cells[0].Value = GrvDanhSachNV.Rows.Count + 1;
                    rows.Cells[1].Value = row["TenDangNhap"].ToString();
                    rows.Cells[2].Value = row["HoVaTen"].ToString();
                    rows.Cells[3].Value = DateTime.Parse(row["NgaySinh"].ToString()).ToString("d");
                    rows.Cells[4].Value = row["GioiTinh"].ToString() == "1" ? "Nam" : "Nữ";
                    rows.Cells[5].Value = row["DiaChi"].ToString();
                    rows.Cells[6].Value = row["DienThoai"].ToString();
                    rows.Cells[7].Value = row["NhomQuyen"].ToString();
                    rows.Cells[8].Value = row["HinhAnh"].ToString();
                    GrvDanhSachNV.Rows.Add(rows);
                }
            }
        }
        int _option = 0;
        void SetEdit()
        {
            txtMaNV.ReadOnly = true;
            txtDChi.ReadOnly = false;
            txtDThoai.ReadOnly = false;
            txtMKhau.ReadOnly = false;
            txtMKhau_R.Text = txtMKhau.Text;
            txtTenNV.ReadOnly = false;
            linklbInsertPic.Enabled = true;
            linklbDelPic.Enabled = true;
            dTPicker_NSinh.Enabled = true;
            cmdSave.Visible = true;
            cmdCancel.Visible = true;
            GrvDanhSachNV.Enabled = false;
            grpFunction.Enabled = false;
            _option = 2;
        }
        void SetAdd(bool edit)
        {
            picNV.Image = null;
            linklbInsertPic.Tag = "";
            txtMaNV.Text = "";
            txtDChi.Text = "";
            txtDThoai.Text = "";
            txtMKhau.Text = "";
            txtMKhau_R.Text = "";
            txtTenNV.Text = "";
            int year = DateTime.Now.Year - 18;
            dTPicker_NSinh.Value = DateTime.Parse(DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + year.ToString());
            linklbInsertPic.Enabled = edit;
            linklbDelPic.Enabled = edit;
            txtMaNV.ReadOnly = !edit;
            txtDChi.ReadOnly = !edit;
            txtDThoai.ReadOnly = !edit;
            txtMKhau.ReadOnly = !edit;
            txtTenNV.ReadOnly = !edit;
            dTPicker_NSinh.Enabled = edit;
            optNam.Enabled = edit;
            optNu.Enabled = edit;
            txtMKhau_R.Visible = edit;
            lbMKhau_R.Visible = edit;
            cmdSave.Visible = edit;
            cmdCancel.Visible = edit;
            GrvDanhSachNV.Enabled = !edit;
            grpFunction.Enabled = !edit;
            _option = edit ? 1 : 0;
        }
        bool ketqua = false;
        private void GrvDanhSachNV_RowEnter_1(object sender, DataGridViewCellEventArgs e)
        {
            int dong;
            dong = e.RowIndex;
            this.txtTenNV.Text = GrvDanhSachNV.Rows[dong].Cells["HoVaTen"].Value.ToString();
            this.txtMaNV.Text = GrvDanhSachNV.Rows[dong].Cells["TenDangNhap"].Value.ToString();
            this.txtDChi.Text = GrvDanhSachNV.Rows[dong].Cells["DiaChi"].Value.ToString();
            this.txtDThoai.Text = GrvDanhSachNV.Rows[dong].Cells["DienThoai"].Value.ToString();
            dTPicker_NSinh.Text = GrvDanhSachNV.Rows[dong].Cells["NgaySinh"].Value.ToString();
            if (GrvDanhSachNV.Rows[dong].Cells["GioiTinh"].Value.ToString() == "Nam")
            {
                optNam.Checked = true;
            }
            else
            {
                optNu.Checked = true;
            }
            picNV.Image = null;
            if (GrvDanhSachNV.Rows[dong].Cells["HinhAnh"].Value.ToString() != "")
            {
                string path = Application.StartupPath + @"\Images\" + GrvDanhSachNV.Rows[dong].Cells["HinhAnh"].Value.ToString();
                if (File.Exists(path))
                    picNV.Image = Image.FromFile(path);
            }
        }

        private void txtMKhau_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            txtMKhau_R.Text = "";
            txtMKhau_R.Visible = true;
            lbMKhau_R.Visible = true;
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            NhanVien_OBJ obj = new NhanVien_OBJ();
            obj.Option = Convert.ToString(_option);
            if(txtMaNV.Text=="")
            {
                obj.TenDangNhap = "a";
                return;
            }
           else
            {
                obj.TenDangNhap = txtMaNV.Text;
            }
            obj.HovaTen = txtTenNV.Text;
            obj.GioiTinh = optNam.Checked ? "1" : "0";
            obj.DienThoai = txtDThoai.Text;
            obj.MatKhau = txtMKhau.Text;
            obj.NgaySinh = dTPicker_NSinh.Value;
            obj.DiaChi = txtDChi.Text;
            obj.HinhAnh = linklbInsertPic.Tag.ToString().Trim();
            ketqua = BUS.NhanVien_BUS.Customer_Insert_Up(obj);
            if (!ketqua)
            {
                MessageBox.Show("Thêm nhân viên thất bại !");
            }
            else
            {
                MessageBox.Show("Thêm nhân viên thành công!");
                Load_NV();
                SetAdd(false);
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn xoá nhân viên", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                ketqua = _nahnvienBUS.Del(txtMaNV.Text);
                if (!ketqua)
                {
                    MessageBox.Show("Xoá nhân viên thất bại!");
                }
                else
                {
                    MessageBox.Show("Xoá nhân viên thành công!");
                    Load_NV();
                }
            }
        }

        private void linklbDelPic_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            linklbInsertPic.Tag = "";
            picNV.Image = null;
        }

        private void linklbInsertPic_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OFileDialog.ShowDialog();
            if (OFileDialog.FileName == "")
                return;
            string filename;
            filename = OFileDialog.FileName;
            linklbInsertPic.Tag = filename;
            picNV.Image = Image.FromFile(linklbInsertPic.Tag.ToString());
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn thoát hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Application.Exit();
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            SetAdd(false);
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (txtMaNV.Text == "")
                return;
            SetEdit();
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            SetAdd(true);
        }

        private void GrvDanhSachNV_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            for (int i = 0; i < GrvDanhSachNV.RowCount; i++)
                GrvDanhSachNV.Rows[i].Cells[0].Value = i + 1;
        }
    }
}
